//
//  HTMUserDefaultUtil.m
//  QVBusUser
//
//  Created by Ting Han on 2017/7/24.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTMUserDefaultUtil.h"
#import "HTMUserDefaultManager.h"

#import "HTAreaModel.h"


@implementation HTMUserDefaultUtil

+ (BOOL)isLogin{
    NSString *login = [HTMUserDefaultManager readUserDataForKey:@"LOGIN"];
    return login.intValue;
}
+ (void)setLogined{
    [HTMUserDefaultManager saveUserData:@"1" forKey:@"LOGIN"];
}
+ (void)setLginOuted{
    [HTMUserDefaultManager saveUserData:@"0" forKey:@"LOGIN"];
}

+ (NSString *)readUserToken{
    return [HTMUserDefaultManager readUserDataForKey:@"token"];
}
+ (void)saveUserToken:(NSString *)token{
    [HTMUserDefaultManager saveUserData:token forKey:@"token"];
}
/**
 保存用户deviceToken
 */
+ (void)saveDeviceToken:(NSData *)token{
    [HTMUserDefaultManager saveUserData:token forKey:@"deviceToken"];
}
/**
 读取用户deviceToken
 */
+ (NSData *)readDeviceToken{
    return [HTMUserDefaultManager readUserDataForKey:@"deviceToken"];
}

+ (void)setUserPhone:(NSString *)phone{
    [HTMUserDefaultManager saveUserData:phone forKey:@"MyPhone"];
}
+ (NSString *)getUserPhone{
    return  [HTMUserDefaultManager readUserDataForKey:@"MyPhone"];;
}

//历史数据
+ (void)saveHistoryStartArray:(NSMutableArray *)history{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *fileName = [path stringByAppendingPathComponent:@"HistoryStart.plist"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [NSKeyedArchiver archiveRootObject:dic toFile:fileName];
    int i = 0;
    for (HTAreaModel *model in history) {
        [dic setObject:model forKey:[NSNumber numberWithInt:i]];
        i ++;
    }
    [NSKeyedArchiver archiveRootObject:dic toFile:fileName];
}
+ (NSMutableArray *)getHistoryStartArray{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *fileName = [path stringByAppendingPathComponent:@"HistoryStart.plist"];
    NSMutableDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];
    NSMutableArray *array = [NSMutableArray array];
    for (NSNumber *number in dic.allKeys) {
        [array addObject: dic[number]];
    }
    return array;
}
+ (void)saveHistoryDestinationArray:(NSMutableArray *)history{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *fileName = [path stringByAppendingPathComponent:@"HistoryEnd.plist"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [NSKeyedArchiver archiveRootObject:dic toFile:fileName];
    int i = 0;
    for (HTAreaModel *model in history) {
        [dic setObject:model forKey:[NSNumber numberWithInt:i]];
        i ++;
    }
    [NSKeyedArchiver archiveRootObject:dic toFile:fileName];
}
+ (NSMutableArray *)getHistoryDestinationArray{

    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *fileName = [path stringByAppendingPathComponent:@"HistoryEnd.plist"];
    NSMutableDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];
    NSMutableArray *array = [NSMutableArray array];
    for (NSNumber *number in dic.allKeys) {
        [array addObject: dic[number]];
    }
    return array;
}
+ (void)saveHistoryLength:(NSString *)length{
      [HTMUserDefaultManager saveUserData:length forKey:@"historyLength"];
}
+ (NSString *)getHistoryLength{
  return [HTMUserDefaultManager readUserDataForKey:@"historyLength"];
}

//到货信息提示
+ (void)savePlayMusicFlag:(BOOL)flag{
    [HTMUserDefaultManager saveUserIntegerData:flag forKey:@"isPlayMusic"];
}
+ (BOOL)getPlayMusicFlag{
    return  [HTMUserDefaultManager readUserIntegerForKey:@"isPlayMusic"];
}
@end
